package com.itheima.reggie.Impl;

import com.itheima.reggie.CategoryService;
import com.itheima.reggie.common.CustomException;
import com.itheima.reggie.domain.Category;
import com.itheima.reggie.mapper.CategoryMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
@Transactional
public class CategoryServiceImpl implements CategoryService {

    @Autowired
    private CategoryMapper categoryMapper;
    @Override
    public List<Category> findAll() {
        return categoryMapper.findAll();
    }

    @Override
    public void save(Category category) {
        //调用mapper新增
        categoryMapper.insert(category);
    }

    @Override
    public void update(Category category) {
        //调用mapper修改
        categoryMapper.updateById(category);
    }

       @Override
    public void deleteMyId(Long id) {
        //菜系有菜 返回错误
        Integer a = categoryMapper.caipin(id);
        if (a > 0) {
          throw new CustomException("当前菜系有菜，请删除套餐去");
        }
        //套餐有餐 返回错误
        Integer b = categoryMapper.taocan(id);
        if (b > 0) {
            throw new CustomException("当前套餐有餐，请删除菜系去");
        }
        //调用mapper删除
        categoryMapper.deleteMyId(id);
    }



}
